/*
 * BuyDialog.java
 *
 * Created on 25/05/2011, 14:29:08
 */
package src.ui.guiComponents.dialogs;

import comm.MonopolyResult;
import java.awt.BorderLayout;
import javax.swing.JOptionPane;
import src.assets.Asset;
import src.assets.City;
import src.client.GameManager;
import src.client.Server;
import src.ui.guiComponents.Squares.SquarePanel;
import src.ui.guiComponents.Squares.SqurePanelFactory;

/**
 *public class BuyDialog extends javax.swing.JDialog
 * public
 * this is used to prompt player to decide if he wants to buy somthing
 * @author Omer Shenhar and Shachar Butnaro
 */
public class BuyDialog extends javax.swing.JDialog {

    private int eventID;
    private Asset assetToOffer;
    java.awt.Frame parent;

    /**
     * public BuyDialog(java.awt.Frame parent, boolean modal, Asset asset, boolean buyAsset, int eventID) 
     * public
     * @param parent - frame for super
     * @param modal -is modal
     * @param asset- the asset in qustion
     * @param buyAsset- true if u want to buy the asset false for a house
     * @param eventID - the event ID of the prompt event
     */
    public BuyDialog(java.awt.Frame parent, boolean modal, Asset asset, boolean buyAsset, int eventID) {
        super(parent, modal);
        this.parent = parent;
        initComponents();
        initComponentsAdditions(asset, buyAsset);
        this.eventID = eventID;
        assetToOffer = asset;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        titleLabel = new javax.swing.JLabel();
        yesButton = new javax.swing.JButton();
        noButton = new javax.swing.JButton();
        currSquarePanel = new javax.swing.JPanel();
        groupButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

        titleLabel.setText("Do you want to buy ");

        yesButton.setLabel("Yes please");
        yesButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                yesButtonActionPerformed(evt);
            }
        });

        noButton.setLabel("Hell no");
        noButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                noButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout currSquarePanelLayout = new javax.swing.GroupLayout(currSquarePanel);
        currSquarePanel.setLayout(currSquarePanelLayout);
        currSquarePanelLayout.setHorizontalGroup(
            currSquarePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 402, Short.MAX_VALUE)
        );
        currSquarePanelLayout.setVerticalGroup(
            currSquarePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 228, Short.MAX_VALUE)
        );

        groupButton.setText("show Group");
        groupButton.setToolTipText("show asset group");
        groupButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                groupButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(yesButton)
                        .addGap(77, 77, 77)
                        .addComponent(groupButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 88, Short.MAX_VALUE)
                        .addComponent(noButton)
                        .addContainerGap())
                    .addComponent(titleLabel)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(currSquarePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(titleLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(currSquarePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(yesButton)
                    .addComponent(noButton)
                    .addComponent(groupButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void noButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_noButtonActionPerformed
        doBuy(false);
        this.dispose();
    }//GEN-LAST:event_noButtonActionPerformed

    private void yesButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_yesButtonActionPerformed
        doBuy(true);
        this.dispose();
    }//GEN-LAST:event_yesButtonActionPerformed

    private void groupButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_groupButtonActionPerformed
        AssetGroupDialog diag = new AssetGroupDialog(parent, assetToOffer.getGroup());
        diag.setVisible(true);
    }//GEN-LAST:event_groupButtonActionPerformed
    private void doBuy(boolean buy) {
        MonopolyResult res = Server.getInstance().buy(GameManager.clientPlayerID, eventID, buy);
        if (res.isError()) {
            JOptionPane.showMessageDialog(null, res.getErrorMessage().getValue(), "buy op failed - (probly due to timeout))", JOptionPane.ERROR_MESSAGE);
        }
        this.dispose();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel currSquarePanel;
    private javax.swing.JButton groupButton;
    private javax.swing.JButton noButton;
    private javax.swing.JLabel titleLabel;
    private javax.swing.JButton yesButton;
    // End of variables declaration//GEN-END:variables

    private void initComponentsAdditions(Asset asset, boolean buyAsset) {
        String mes = "do you want to buy";
        if (buyAsset) {
            mes += " the Asset " + asset.getName() + " for " + asset.getCost() + "? ";
        } else { //Here buying house in asset
            mes += " house number " + (((City) asset).getNumHouses()+1) + " at " + asset.getName() + " for " + ((City) asset).getCostOfHouse() + "? ";
        }
        this.setTitle(mes);
        titleLabel.setText(mes);
        SquarePanel panel = SqurePanelFactory.makeCorrectSqurePanel(asset, false);
        panel.setVisible(true);
        currSquarePanel.setLayout(new BorderLayout());
        currSquarePanel.add(panel, BorderLayout.CENTER);
        currSquarePanel.setVisible(true);
        this.validate();
        this.repaint();
    }
}
